local joy__huanshu = fk.CreateSkill {

  name = "joy__huanshu",

  tags = { Skill.Compulsory, },

}



joy__huanshu:addEffect(fk.RoundStart, {
  name = "joy__huanshu",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.RoundStart, fk.Damaged, fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__huanshu.name) then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Play and not player:isKongcheng()
    elseif event == fk.AfterCardsMove then
      local cards = {}
      for _, move in ipairs(data) do
        if move.to and move.to ~= player.id and move.toArea == Player.Hand then
          local to = player.room:getPlayerById(move.to)
          if not to:hasSkill(joy__huanshu.name) then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId):getMark("@@joy__huanshu_card") ~= 0 and table.contains(to.player_cards[Player.Hand], info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return (event == fk.RoundStart or target == player)
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local n = event == fk.Damaged and data.damage or 1
    for i = 1, n do
      if player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local cards, map = {}, {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local c = Fk:getCardById(id)
        if c:getMark("@@joy__huanshu_card") == 1 and c:getMark("@@joy__huanhua_card-inhand") == 0 then
          table.insert(cards, id)
          if map[c.suit] == nil then
            map[c.suit] = {id}
          else
            table.insert(map[c.suit], id)
          end
        end
      end
      if #cards == 0 then return end
      --- FIXME:暂无不产生移动的换牌方式，或者直接改变牌位置的方式
      room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if player.dead then return end
      local pile = GetHuanPile(room)
      local get = {}
      while #pile > 0 and #cards > 0 do
        local t = table.remove(pile, math.random(#pile))
        local suit = Fk:getCardById(t, true).suit
        if map[suit] and #map[suit] > 0 then
          table.insert(get, t)
          table.remove(map[suit], 1)
          table.remove(cards, 1)
        end
      end
      if #get > 0 then
        room:delay(300)
        -- FIXME:心变智慧，防止从虚空区拿出id为-1的暗牌
        for _, id in ipairs(get) do
          table.removeOne(room.void, id)
          room:setCardArea(id, Card.DrawPile)
        end
        room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, joy__huanshu.name)
      end
    elseif event == fk.AfterCardsMove then
      room:moveCardTo(event:getCostData(self), Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if not player.dead then
        player:drawCards(1, joy__huanshu.name)
      end
    else
      GetHuanCard (player, 2, joy__huanshu.name)
    end
  end,
})
joy__huanshu:addEffect(fk.Damaged, {
  name = "joy__huanshu",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.RoundStart, fk.Damaged, fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__huanshu.name) then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Play and not player:isKongcheng()
    elseif event == fk.AfterCardsMove then
      local cards = {}
      for _, move in ipairs(data) do
        if move.to and move.to ~= player.id and move.toArea == Player.Hand then
          local to = player.room:getPlayerById(move.to)
          if not to:hasSkill(joy__huanshu.name) then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId):getMark("@@joy__huanshu_card") ~= 0 and table.contains(to.player_cards[Player.Hand], info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return (event == fk.RoundStart or target == player)
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local n = event == fk.Damaged and data.damage or 1
    for i = 1, n do
      if player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local cards, map = {}, {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local c = Fk:getCardById(id)
        if c:getMark("@@joy__huanshu_card") == 1 and c:getMark("@@joy__huanhua_card-inhand") == 0 then
          table.insert(cards, id)
          if map[c.suit] == nil then
            map[c.suit] = {id}
          else
            table.insert(map[c.suit], id)
          end
        end
      end
      if #cards == 0 then return end
      --- FIXME:暂无不产生移动的换牌方式，或者直接改变牌位置的方式
      room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if player.dead then return end
      local pile = GetHuanPile(room)
      local get = {}
      while #pile > 0 and #cards > 0 do
        local t = table.remove(pile, math.random(#pile))
        local suit = Fk:getCardById(t, true).suit
        if map[suit] and #map[suit] > 0 then
          table.insert(get, t)
          table.remove(map[suit], 1)
          table.remove(cards, 1)
        end
      end
      if #get > 0 then
        room:delay(300)
        -- FIXME:心变智慧，防止从虚空区拿出id为-1的暗牌
        for _, id in ipairs(get) do
          table.removeOne(room.void, id)
          room:setCardArea(id, Card.DrawPile)
        end
        room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, joy__huanshu.name)
      end
    elseif event == fk.AfterCardsMove then
      room:moveCardTo(event:getCostData(self), Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if not player.dead then
        player:drawCards(1, joy__huanshu.name)
      end
    else
      GetHuanCard (player, 2, joy__huanshu.name)
    end
  end,
})
joy__huanshu:addEffect(fk.EventPhaseStart, {
  name = "joy__huanshu",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.RoundStart, fk.Damaged, fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__huanshu.name) then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Play and not player:isKongcheng()
    elseif event == fk.AfterCardsMove then
      local cards = {}
      for _, move in ipairs(data) do
        if move.to and move.to ~= player.id and move.toArea == Player.Hand then
          local to = player.room:getPlayerById(move.to)
          if not to:hasSkill(joy__huanshu.name) then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId):getMark("@@joy__huanshu_card") ~= 0 and table.contains(to.player_cards[Player.Hand], info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return (event == fk.RoundStart or target == player)
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local n = event == fk.Damaged and data.damage or 1
    for i = 1, n do
      if player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local cards, map = {}, {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local c = Fk:getCardById(id)
        if c:getMark("@@joy__huanshu_card") == 1 and c:getMark("@@joy__huanhua_card-inhand") == 0 then
          table.insert(cards, id)
          if map[c.suit] == nil then
            map[c.suit] = {id}
          else
            table.insert(map[c.suit], id)
          end
        end
      end
      if #cards == 0 then return end
      --- FIXME:暂无不产生移动的换牌方式，或者直接改变牌位置的方式
      room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if player.dead then return end
      local pile = GetHuanPile(room)
      local get = {}
      while #pile > 0 and #cards > 0 do
        local t = table.remove(pile, math.random(#pile))
        local suit = Fk:getCardById(t, true).suit
        if map[suit] and #map[suit] > 0 then
          table.insert(get, t)
          table.remove(map[suit], 1)
          table.remove(cards, 1)
        end
      end
      if #get > 0 then
        room:delay(300)
        -- FIXME:心变智慧，防止从虚空区拿出id为-1的暗牌
        for _, id in ipairs(get) do
          table.removeOne(room.void, id)
          room:setCardArea(id, Card.DrawPile)
        end
        room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, joy__huanshu.name)
      end
    elseif event == fk.AfterCardsMove then
      room:moveCardTo(event:getCostData(self), Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if not player.dead then
        player:drawCards(1, joy__huanshu.name)
      end
    else
      GetHuanCard (player, 2, joy__huanshu.name)
    end
  end,
})
joy__huanshu:addEffect(fk.AfterCardsMove, {
  name = "joy__huanshu",
  anim_type = "control",
  frequency = Skill.Compulsory,
  events = {fk.RoundStart, fk.Damaged, fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__huanshu.name) then return false end
    if event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Play and not player:isKongcheng()
    elseif event == fk.AfterCardsMove then
      local cards = {}
      for _, move in ipairs(data) do
        if move.to and move.to ~= player.id and move.toArea == Player.Hand then
          local to = player.room:getPlayerById(move.to)
          if not to:hasSkill(joy__huanshu.name) then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId):getMark("@@joy__huanshu_card") ~= 0 and table.contains(to.player_cards[Player.Hand], info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    else
      return (event == fk.RoundStart or target == player)
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local n = event == fk.Damaged and data.damage or 1
    for i = 1, n do
      if player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local cards, map = {}, {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        local c = Fk:getCardById(id)
        if c:getMark("@@joy__huanshu_card") == 1 and c:getMark("@@joy__huanhua_card-inhand") == 0 then
          table.insert(cards, id)
          if map[c.suit] == nil then
            map[c.suit] = {id}
          else
            table.insert(map[c.suit], id)
          end
        end
      end
      if #cards == 0 then return end
      --- FIXME:暂无不产生移动的换牌方式，或者直接改变牌位置的方式
      room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if player.dead then return end
      local pile = GetHuanPile(room)
      local get = {}
      while #pile > 0 and #cards > 0 do
        local t = table.remove(pile, math.random(#pile))
        local suit = Fk:getCardById(t, true).suit
        if map[suit] and #map[suit] > 0 then
          table.insert(get, t)
          table.remove(map[suit], 1)
          table.remove(cards, 1)
        end
      end
      if #get > 0 then
        room:delay(300)
        -- FIXME:心变智慧，防止从虚空区拿出id为-1的暗牌
        for _, id in ipairs(get) do
          table.removeOne(room.void, id)
          room:setCardArea(id, Card.DrawPile)
        end
        room:moveCardTo(get, Card.PlayerHand, player, fk.ReasonPrey, joy__huanshu.name)
      end
    elseif event == fk.AfterCardsMove then
      room:moveCardTo(event:getCostData(self), Card.Void, nil, fk.ReasonJustMove, joy__huanshu.name)
      if not player.dead then
        player:drawCards(1, joy__huanshu.name)
      end
    else
      GetHuanCard (player, 2, joy__huanshu.name)
    end
  end,
})

joy__huanshu:addEffect("maxcards", {
  name = "#joy__huanshu_maxcards",
  exclude_from = function(self, player, card)
    return player:hasSkill(joy__huanshu.name) and card and card:getMark("@@joy__huanshu_card") ~= 0
  end,
})

return joy__huanshu